#include"preCmpH.h"
#define WIN32_LEAN_AND_MEAN
#include<Windows.h>
#include "UITextbox.h"
#include"UIFontManager.h"
#include"UIRenderer.h"

BEGIN_NAMESPACE(ui_manager)
BEGIN_NAMESPACE(ui_controls)

UITextbox::UITextbox()
: text()
, enabled_(true)
, index(0)
, c_draw(false)
{
}
UITextbox::~UITextbox()
{
}
void UITextbox::BoundBy(UIManager& manager, bool is_component)
{
	UIControl::BoundBy(manager,is_component);
	text.BoundBy(manager, true);
	size_.setWidth(100);
	size_.setHeight(text.GetFont().GetSize() * (96.0f / 64.0f));
	
	if(background_ == null){
		background_ = &mcInterface_.getUIRenderer().GenerateImage("uitextbox_standard",size_.getWidth(),size_.getHeight(),false,0xffffffff);
	}
	bound_ = true;
}
void UITextbox::Render(UIPoint parentPosistion)
{
	if(text.GetText().length() > 0){
		if((uint)index >= text.GetText().length())
		{
			mcInterface_.getUIFontManager().LoadText(text.GetFont(),text.GetText());
			ui_fonts::UIFontChar temp = mcInterface_.getUIFontManager().GetChar(index - 1);
			c_x = pos_.getX() + temp.GetPosX() + temp.GetWidth();
			c_y = pos_.getY() + size_.getHeight();
		} else {
			mcInterface_.getUIFontManager().LoadText(text.GetFont(),text.GetText());
			ui_fonts::UIFontChar temp = mcInterface_.getUIFontManager().GetChar(index);
			c_x = pos_.getX() + temp.GetPosX();
			c_y = pos_.getY() + size_.getHeight();
		}
	} else {
		c_x = pos_.getX();
		c_y = pos_.getY() + size_.getHeight();
	}

	mcInterface_.getUIRenderer().DrawImage(
		pos_.getX(),pos_.getY(),
		size_.getWidth(), size_.getHeight(),
		*background_);

	if(c_draw && focused_){
		mcInterface_.getUIRenderer().DrawLine(
						c_x, pos_.getY(),
						c_x, c_y, 0);
	}
}
void UITextbox::HandleEvent(ui_events::UIEvent ui_event)
{
	switch (ui_event.getMsgFlag())
	{
		case ui_events::UIEvent::FLAG_KEYBOARD:
			{
				if(focused_){
					if( ui_event.getKeyboardKey() == ui_events::ui_input::KEY_LEFT &&
						ui_event.getKeyboardKeyState() == ui_events::ui_input::ACTION_DOWN){
						if(index > 0){
							--index;
						}
					}
					if( ui_event.getKeyboardKey() == ui_events::ui_input::KEY_RIGHT &&
						ui_event.getKeyboardKeyState() == ui_events::ui_input::ACTION_DOWN){
						if( (uint)index < text.GetText().length()){
							++index;
						}
					}
					if( ui_event.getKeyboardKey() == ui_events::ui_input::KEY_BACKSPACE &&
						ui_event.getKeyboardKeyState() == ui_events::ui_input::ACTION_DOWN){
						if(index > 0){
							text.SetText(text.GetText().erase(index - 1,1));
							if(!text.getBackground().IsEmpty()){
								text.getBackground().Clear();
							}
							--index;
						}
					}
					if(ui_event.getKeyboardKeyState() == ui_events::ui_input::ACTION_DOWN &&
						ui_event.getKeyboardKey() >= 'A' && ui_event.getKeyboardKey() <= 'Z'){
							text.SetText(text.GetText() + (char)(ui_event.getKeyboardKey() + 32));
							++index;
							if(!text.getBackground().IsEmpty()){
								text.getBackground().Clear();
							}
					}
				}
			}
			break;
		case ui_events::UIEvent::FLAG_MOUSE:
			{
				UIPoint mouse_pos = ui_event.getMousePosition(true);
				if((mouse_pos.getX() > pos_.getX() && mouse_pos.getX() < pos_.getX() + size_.getWidth()) &&
					(mouse_pos.getY() > pos_.getY() && mouse_pos.getY() < pos_.getY() + size_.getHeight())){
						   if(ui_event.getMouseButton() == ui_events::ui_input::BUTTON_LEFT &&
							   ui_event.getMouseButtonState() == ui_events::ui_input::ACTION_DOWN){
							   setFocus(*this);
							   index = text.GetText().length();
						   }
					}
			}
			break;
		case ui_events::UIEvent::FLAG_RENDER:
			{
				UIPoint render_pos = ui_event.getRenderParentPosition();
				render_pos.setX( render_pos.getX() + pos_.getX());
				render_pos.setY( render_pos.getY() + pos_.getY());
				Render(render_pos);
				mcInterface_.getUIRenderer().setClipping(
								  pos_.getX(),
								  pos_.getY(),
								  size_.getWidth(),
								  size_.getHeight());
				ui_event.setRenderMsg(pos_.getX(),pos_.getY(),true);
			}
			break;
		case ui_events::UIEvent::FLAG_TICK:
			{
				if(!c_draw){
					c_draw = true;
				} else {
					c_draw = false;
				}
			}
			break;
		default:
			break;
	}
	text.HandleEvent(ui_event);
	mcInterface_.getUIRenderer().DisableClipping();
}


END_NAMESPACE // ui controls
END_NAMESPACE // ui manager